<?php

/**
 * This is the model class for table "ticket".
 *
 * The followings are the available columns in table 'ticket':
 * @property integer $id
 * @property string $ticket_name
 * @property string $ticket_descript
 * @property string $ticket_service_type
 * @property string $effective_date
 * @property string $demo_duration
 * @property string $action_done
 * @property string $recommendation
 * @property integer $employee_id
 * @property integer $customer_business_id
 * @property integer $customer_resident_id
 */
class Ticket extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ticket';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ticket_name, ticket_descript, ticket_service_type, effective_date, employee_id', 'required'),
			array('employee_id, customer_business_id, customer_resident_id', 'numerical', 'integerOnly'=>true),
			array('ticket_name, ticket_service_type', 'length', 'max'=>45),
			array('ticket_descript', 'length', 'max'=>100),
			array('recommendation', 'length', 'max'=>600),
			array('demo_duration', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, ticket_name, ticket_descript, ticket_service_type, effective_date, demo_duration, recommendation, employee_id, customer_business_id, customer_resident_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'customerBusiness' => array(self::BELONGS_TO, 'CustomerBusiness', 'customer_business_id'),
			'customerResident' => array(self::BELONGS_TO, 'CustomerResident', 'customer_resident_id'),
			'employee' => array(self::BELONGS_TO, 'Employee', 'employee_id'),
			'subcons' => array(self::MANY_MANY, 'Subcon', 'ticket_has_subcon(ticket_id, subcon_id)'),
			'ticketStatuses' => array(self::HAS_MANY, 'TicketStatus', 'ticket_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'ticket_name' => 'Ticket Name',
			'ticket_descript' => 'Ticket Description',
			'ticket_service_type' => 'Ticket Service Type',
			'effective_date' => 'Effective Date',
			'demo_duration' => 'Demo Duration',
			'recommendation' => 'Recommendation',
			'employee_id' => 'Employee',
			'customer_business_id' => 'Customer Corporate',
			'customer_resident_id' => 'Customer Sm Tenants',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		//$criteria->compare('id',$this->id);
		$criteria->compare('ticket_name',$this->ticket_name,true);
		$criteria->compare('ticket_descript',$this->ticket_descript,true);
		$criteria->compare('ticket_service_type',$this->ticket_service_type,true);
		$criteria->compare('effective_date',$this->effective_date,true);
		$criteria->compare('demo_duration',$this->demo_duration,true);
		$criteria->compare('recommendation',$this->recommendation,true);
		//$criteria->compare('employee_id',$this->employee_id);
		//$criteria->compare('customer_business_id',$this->customer_business_id);
		//$criteria->compare('customer_resident_id',$this->customer_resident_id);
		
		$criteria->compare('t.id',$this->id);
$criteria->compare('employee.employee_lname',$this->employee_id, true);


$criteria->compare('t.id',$this->id);
$criteria->compare('customerBusiness.cust_lname',$this->customer_business_id, true);



$criteria->compare('t.id',$this->id);
$criteria->compare('customerResident.person_lname',$this->customer_resident_id, true);
$criteria->with=array('customerResident','customerBusiness','employee');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Ticket the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
